package com.yonyou.brigade.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCConfig;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCOrderService;
import com.yonyou.brigade.util.resultutil.Msg;
import com.yonyou.brigade.util.resultutil.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cron")
public class GetSettleOrderController {


    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("ctripBCOrderServiceImpl")
    private ICtripBCOrderService ctripBCOrderService;

    /**
     * 手动拉取携程机票结算明细订单
     * @param
     * @return
     */
    @RequestMapping(value = "/ctripBC/getFlightSettleOrders", method = RequestMethod.POST)
    public Msg getCtripFlightSettlementOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            List<String> channelConfigList = new ArrayList<>();
            String begin = param.getString("begin");
            String end = param.getString("end");
            for (BriChannel briChannel : briChannels) {
                CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                if (channelConfigList.contains(config.getAppKey())){
                    continue;
                } else {
                    channelConfigList.add(config.getAppKey());
                }
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                ctripBCOrderService.getCtripBCFlightSettleOrders(tenant, orgpk, begin, end, briChannel.getId(), config);
            }
        }
        return ResultUtil.success();
    }

    /**
     * 手动拉取携程酒店结算明细订单
     * @param
     * @return
     */
    @RequestMapping(value = "/ctripBC/getHotelSettleOrders", method = RequestMethod.POST)
    public Msg getCtripHotelSettlementOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            List<String> channelConfigList = new ArrayList<>();

            String begin = param.getString("begin");
            String end = param.getString("end");
            for (BriChannel briChannel : briChannels) {
                CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                if (channelConfigList.contains(config.getAppKey())){
                    continue;
                } else {
                    channelConfigList.add(config.getAppKey());
                }
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                ctripBCOrderService.getCtripBCHotelSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
            }
        }
        return ResultUtil.success();
    }


    /**
     * 手动拉取携程火车票结算明细订单
     * @param
     * @return
     */
    @RequestMapping(value = "/ctripBC/getTrainSettleOrders", method = RequestMethod.POST)
    public Msg getCtripTrainSettlementOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            List<String> channelConfigList = new ArrayList<>();
            String begin = param.getString("begin");
            String end = param.getString("end");
            for (BriChannel briChannel : briChannels) {
                CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                if (channelConfigList.contains(config.getAppKey())){
                    continue;
                } else {
                    channelConfigList.add(config.getAppKey());
                }
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                ctripBCOrderService.getCtripBCTrainSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
            }
        }
        return ResultUtil.success();
    }


    /**
     * 手动拉取携程用车结算明细订单
     * @param
     * @return
     */
    @RequestMapping(value = "/ctripBC/getCarSettleOrders", method = RequestMethod.POST)
    public Msg getCtripCarSettlementOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            List<String> channelConfigList = new ArrayList<>();
            String begin = param.getString("begin");
            String end = param.getString("end");
            for (BriChannel briChannel : briChannels) {
                CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                if (channelConfigList.contains(config.getAppKey())){
                    continue;
                } else {
                    channelConfigList.add(config.getAppKey());
                }
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                ctripBCOrderService.getCtripBCCarSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
            }
        }
        return ResultUtil.success();
    }

}
